package com.legocms.data.entities.cms;

import com.legocms.core.common.StringUtil;
import com.legocms.data.base.BaseEntity;
import com.legocms.data.entities.sys.SysSite;
import com.legocms.data.entities.sys.SysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Entity
@Table(name = "cms_content")
@EqualsAndHashCode(callSuper = true)
public class CmsContent extends BaseEntity {
    private String title;
    private String copied;
    private String author;
    private String editor;
    private String description;
    private String cover;
    private String source;
    private String sourceUrl;
    private String text;
    private Date updateTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    private CmsContent parent;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site_id", referencedColumnName = "id")
    private SysSite site;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private SysUser user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "check_user_id", referencedColumnName = "id")
    private SysUser checkUser;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    private CmsCategory category;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "model_id", referencedColumnName = "id")
    private CmsModel model;

    protected CmsContent() { }

    public CmsContent(String code) {
        super(code);
    }

    @Override
    protected void doBuildReadableSnapshot(Map<String, String> attributes) {
        attributes.put("编码", getCode());
        attributes.put("名称", getName());
        attributes.put("标题", title);
        attributes.put("所属站点", site.toString());
        attributes.put("发表用户", user.toString());
        attributes.put("审核用户", checkUser.toString());
        String parentName = "";
        if (parent != null) {
            parentName = parent.getName();
        }
        attributes.put("父内容", parentName);
        attributes.put("分类", category.toString());
        attributes.put("内容模型", model.toString());
        attributes.put("转载", copied);
        attributes.put("作者", author);
        attributes.put("编辑", editor);
        attributes.put("描述", description);
        attributes.put("封面", cover);
        attributes.put("来源", source);
        attributes.put("来源网址", sourceUrl);
        attributes.put("正文", text);
    }
}
